package Facade;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import Entity.Offer;

/**
 * Session Bean implementation class OfferFacade
 */
@Stateless
public class OfferFacade implements OfferFacadeRemote {
	@PersistenceContext(unitName = "POLEINFODB")
	protected EntityManager entityMgr;

	public Offer addOffer(Offer o) {
		entityMgr.persist(o);
		return o;
	}

	public void clearAllOffers() {
		Collection<Offer> Offers = listOffers();
		for(Offer nextOffer : Offers){
			entityMgr.remove(nextOffer);
		}
	}

	public void clearOffer(int id_offer) {
		Offer o =entityMgr.find(Offer.class, id_offer);
		entityMgr.remove(o);
	}

	@SuppressWarnings("unchecked")
	public Collection<Offer> listOffers() {
		return entityMgr.createQuery("SELECT o FROM Offer as o").getResultList();
	}


}
